package org.ezplatform.office.meeting.common.util;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

public class MeettingUrlUtils {

	public static String sendPost(String url, Map<String,String> param) {
	    String result = "";
	    List<NameValuePair> list = new ArrayList<NameValuePair>();
	    String fullurl = "";
	    //请求参数
	    for(String key:param.keySet()){
	    	list.add(new BasicNameValuePair(key, param.get(key)));
	    }
	    try{
	    	fullurl = EntityUtils.toString(new UrlEncodedFormEntity(list,Consts.UTF_8));
	    }catch(Exception e){
	    	result = "转换失败";
	    	e.printStackTrace();
	    }
	    HttpPost httppost = new HttpPost(url+"?"+fullurl);
	    CloseableHttpClient httpClient = HttpClients.createDefault();
	    try{
	    	CloseableHttpResponse response = httpClient.execute(httppost);
	    	if(response.getStatusLine().getStatusCode() == 200){
	    		HttpEntity entity = response.getEntity();
	    		result = EntityUtils.toString(entity);
	    	}else{
	    		result = "转换失败";
	    	}
	    }catch (Exception e) {
	    	result = "转换失败";
	    	e.printStackTrace();
	    }finally{
	    	httppost.releaseConnection();
	    }
	    return result;
	}
}
